/**
 * @description 富文本组件
 * @date 2024.9.10
 * @author wz
 * @midify 2025.5.15
 * @editor wz
 */
import { _decorator, Component, EventHandler, EventTouch, Label, Node, RichText, Sprite, UITransform, v2, v3 } from 'cc';
const { ccclass, property } = _decorator;
export interface ISegment {
    node: Node;
    comp: Label | Sprite | null;
    lineCount: number;
    styleIndex: number;
    imageOffset: string;
    clickParam: string;
    clickHandler: string;
    type: string;
}
@ccclass('CRichText')
export class CRichText extends RichText {

    @property({ type: [EventHandler], tooltip: '点击对应位置相应事件' })
    clickEvents: EventHandler[] = [];

    start() {
        this.node.on(Node.EventType.TOUCH_END, this.onRichTextClicked, this);
    }


    getSegmentInfo(index: number): ISegment {
        return this._segments[index]
    }

    onRichTextClicked(event: EventTouch) {
        console.log('***' + this.string.length)

        const clickPosition = event.getUILocation();
        const segments = this._segments;

        for (let i = 0; i < segments.length; i++) {
            const segment = segments[i];
            const localPos = segment.node.parent.getComponent(UITransform).convertToNodeSpaceAR(v3(clickPosition.x, clickPosition.y))
            const boundingBox = segment.node.getComponent(UITransform).getBoundingBox();

            if (boundingBox.contains(v2(localPos.x, localPos.y))) {
                console.log(`i:${i},styleIndex:${segment.styleIndex}`);
                this.clickEvents.forEach((a) => {
                    a.emit([this.node, i, segments])
                })
                break;
            }
        }
    }
}


